package com.dd.dao;

// default package

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.DdOrder;
import com.dd.util.PageList;

/**
 * A data access object (DAO) providing persistence and search support for
 * DdOrder entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .DdOrder
 * @author MyEclipse Persistence Tools
 */

public class DdOrderDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdOrderDAO.class);

	public void save(DdOrder transientInstance) {
		log.debug("saving DdOrder instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DdOrder persistentInstance) {
		log.debug("deleting DdOrder instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void update(DdOrder persistentInstance) {
		log.debug("deleting DdOrder instance");
		try {
			this.getHibernateTemplate().update(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdOrder findById(java.lang.Integer id) {
		log.debug("getting DdOrder instance with id: " + id);
		try {
			DdOrder instance = (DdOrder) this.getHibernateTemplate().load(
					DdOrder.class, id);
			return instance;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("get failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all DdOrder instances");
		try {
			String queryString = "from DdOrder";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public PageList findAllRemark(int sid,int pageNo, int pageSize) {
		log.debug("finding all Remark instances");
		try {
			
			PageList pl = new PageList();
			String orderby = " order by confirmdate desc";
			String sql = "from DdOrder where status=3 and shopid="+sid;
			String maxsizeStr = (" select count(*) " + sql);
			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(maxsizeStr.replace("DdOrder",
					"Dd_Order"));
			int max = 0;
			while (rs.next()) {
				max = rs.getInt(1);
			}
			Query q = this.getSession().createQuery(sql+orderby);
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
			
		} catch (Exception re) {
			
			re.printStackTrace();
			return null;
		}
	}
	public float findSumAll(String sqlwhere) {
		log.debug("finding all DdOrder instances");
		try {
			String queryString = "from Dd_Order where 1=1 " + sqlwhere;
			String maxsizeStr = (" select sum(price) " + queryString);
			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(maxsizeStr);
			float max = 0F;
			while (rs.next()) {
				max = rs.getFloat(1);
			}
			return max;
		} catch (Exception re) {
			re.printStackTrace();
			log.error("findSumAll failed", re);
			return 0F;
		}
	}

	public PageList findAll(String sqlwhere, int pageNo, int pageSize) {
		log.debug("finding all DdOrder instances");
		try {
			PageList pl = new PageList();
			String queryString = "from DdOrder a,DdShop b where a.shopid=b.id  "
					+ sqlwhere;
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery(
					queryString + " order by a.id desc,a.orderdate desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
	public PageList findAllCount(String sqlwhere, int pageNo, int pageSize) {
		log.debug("finding all DdCount instances");
		try {
			PageList pl = new PageList();
			String queryString = "from DdCount where 1=1  "
					+ sqlwhere;
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery(
					queryString + " order by id desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findAllGroupByShop(String sqlwhere) {
		log.debug("finding all DdOrder instances");
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select b.types as types,count(*) as num,sum(a.price) as amount  from dd_order a,dd_shop b  where a.shopid=b.id "
							+ sqlwhere + " group by b.types");
			
			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sbf.toString());
			List list = new ArrayList();
			while (rs.next()) {
				List tmpList = new ArrayList();
				tmpList.add(rs.getString("types"));
				tmpList.add(rs.getInt("num"));
				tmpList.add(rs.getFloat("amount"));
				list.add(tmpList);
			}
			rs.close();
			stat.close();
			return list;
		} catch (Exception re) {
			re.printStackTrace();
			log.error("find all failed", re);
			return null;
		}
	}

	public List findDetailGroupByShop(String sqlwhere,String types) {
		log.debug("finding all DdOrder instances");
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select b.name as name,count(*) as num,sum(price) as amount    from dd_order a,dd_shop b  where a.shopid=b.id "
							+ sqlwhere + " and types="+types+" group by b.id");
			System.out.println(sbf.toString());
			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sbf.toString());
			List list = new ArrayList();
			while (rs.next()) {
				List tmpList = new ArrayList();
				tmpList.add(rs.getString("name"));
				tmpList.add(rs.getInt("num"));
				tmpList.add(rs.getFloat("amount"));
				list.add(tmpList);
			}
			rs.close();
			stat.close();
			return list;
		} catch (Exception re) {
			re.printStackTrace();
			log.error("find all failed", re);
			return null;
		}
	}
	
	public List findAllGroupByYwMain(String sqlwhere) {
		log.debug("finding all DdOrder instances");
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select b.belong as belong,count(*) as num,sum(price) as amount  from dd_order a,dd_shop b  where a.shopid=b.id "
							+ sqlwhere + " group by b.belong");

			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sbf.toString());
			List list = new ArrayList();
			while (rs.next()) {
				List tmpList = new ArrayList();
				tmpList.add(rs.getInt("belong"));
				tmpList.add(rs.getInt("num"));
				tmpList.add(rs.getFloat("amount"));
				list.add(tmpList);
			}
			rs.close();
			stat.close();
			return list;
		} catch (Exception re) {
			re.printStackTrace();
			log.error("find all failed", re);
			return null;
		}
	}

	
	public List findAllGroupByYw(String sqlwhere,String belong) {
		log.debug("finding all DdOrder instances");
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select b.types as types,count(*) as num,sum(price) as amount  from dd_order a,dd_shop b  where a.shopid=b.id "
							+ sqlwhere + " and b.belong="+belong+" group by b.types");

			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sbf.toString());
			List list = new ArrayList();
			while (rs.next()) {
				List tmpList = new ArrayList();
				tmpList.add(rs.getString("types"));
				tmpList.add(rs.getInt("num"));
				tmpList.add(rs.getFloat("amount"));
				list.add(tmpList);
			}
			rs.close();
			stat.close();
			return list;
		} catch (Exception re) {
			re.printStackTrace();
			log.error("find all failed", re);
			return null;
		}
	}

	public List findDetailGroupByYw(String sqlwhere,String types) {
		log.debug("finding all DdOrder instances");
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select b.name as name,count(*) as num,sum(price) as amount  from dd_order a,dd_shop b  where a.shopid=b.id "
							+ sqlwhere + " and types="+types+" group by b.id");

			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sbf.toString());
			List list = new ArrayList();
			while (rs.next()) {
				List tmpList = new ArrayList();
				tmpList.add(rs.getString("name"));
				tmpList.add(rs.getInt("num"));
				tmpList.add(rs.getFloat("amount"));
				list.add(tmpList);
			}
			rs.close();
			stat.close();
			return list;
		} catch (Exception re) {
			re.printStackTrace();
			log.error("find all failed", re);
			return null;
		}
	}
	
	public int findCountByUid(Integer uid) {
		log.debug("finding all DdOrder instances");
		try {
			String maxsizeStr = (" select count(*) from DdOrder where userid="+uid+" and status in (2,3)");
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			return max;
		} catch (RuntimeException re) {
			return 0;
		}
	}
	public int findCountByUid2(Integer uid) {
		log.debug("finding all DdOrder instances");
		try {
			String maxsizeStr = (" select count(*) from DdOrder where userid="+uid+" and status not in (2,3)");
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			return max;
		} catch (RuntimeException re) {
			return 0;
		}
	}
	public List find(Integer orderid) {
		log.debug("finding all DdOrder instances");
		try {
			String queryString = "from DdOrder a,DdShop b where a.shopid=b.id  and a.id="
					+ orderid.intValue();
			Query q = this.getSession().createQuery(
					queryString + " order by a.orderdate desc");
			return q.list();
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	// 未及时付款
	public List findNotPay(String date) {
		log.debug("findNotPay");
		try {
			return this.getHibernateTemplate().find(
					"from DdOrder where orderdate<'" + date + "' and status=0");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	// 商家未及时确认
	public List findNotConfirmS(String date) {
		log.debug("findNotPay");
		try {
			return this.getHibernateTemplate().find(
					"from DdOrder where paydate<'" + date + "' and status=1");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	// 用户未及时确认消费成功--定礼品
	public List findNotConfirmU1(String date) {
		log.debug("findNotPay");
		try {
			return this
					.getHibernateTemplate()
					.find(
							"from DdOrder a,DdUserorder b where a.id=b.orderid and SUBSTRING(b.senddate,1,10)<'"
									+ date + "' and a.status=2");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	// 用户未及时确认消费成功--定位置
	public List findNotConfirmU2(String date) {
		log.debug("findNotPay");
		try {
			return this.getHibernateTemplate().find(
					"from DdOrder a,DdUserorder1 b where a.id=b.orderid and b.orderdate1<'"
							+ date + "' and a.status=2");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
}